package com.example.zhangwei.util;

import java.lang.ref.SoftReference;

import com.example.zhangwei.widgts.CustomToast;

import android.content.Context;
import android.view.Gravity;
import android.widget.Toast;


public final class ToastMaster {
	
	/**
	 *  Show some message for user.<br>
	 *  
	 *  we guarantee that It is shown one time in its duration .<br>
	 *  
	 *  If the message by msgResId  do not exist , we do not show anything .<br>
	 * 
	 * @param context
	 * @param msgResId
	 * @param durationType How long to display the message.  Either LENGTH_SHORT or  LENGTH_LONG
	 */
	public void makeText(Context context, int msgResId, final int durationType){
			makeText(context, context.getApplicationContext().getResources().getString(msgResId), durationType);
	}
	
	/**
	 * Show some message for user.<br>
	 * 
	 * we guarantee that It is shown one time in its duration .<br>
	 * 
	 *  If the message is empty ( null or empty string) , we do not show anything .<br>
	 * 
	 * @param context
	 * @param message
	 * @param duration How long to display the message.  Either LENGTH_SHORT or  LENGTH_LONG
	 */
	public void makeText(Context context, String message, final int durationType){
		
		// make the duration 
		long duration;
		if(durationType == Toast.LENGTH_LONG){
			duration = 3000;// this is a  default long time in framework 
		}else{
			duration = 2000;// this is a  default short  time in framework 
		}
		
		// whether or not show 
		if(isLastMessage(message)  ){
			long now = System.currentTimeMillis();
			long fff = (now-lastTime);
			if(fff>duration){
//				if (mToast != null)
//					mToast.cancel();
				mToast = new CustomToast(context, "标题", message);
				mToast.setDuration(durationType);
				mToast.setGravity(Gravity.CENTER, 0, 0); 
				mToast.show();
			    lastTime = now;
			}
		}else{
//			if (mToast != null)
//				mToast.cancel();
			mToast = new CustomToast(context, "标题", message);
			mToast.setDuration(durationType);
			mToast.setGravity(Gravity.CENTER, 0, 0); 
			mToast.show();
			lastTime = System.currentTimeMillis();
		}
	}
	
	/**
	 * Whether or not current message is the same with the last message .<br>
	 * 
	 * If not , we will set the current message as new message , and put into SoftReference
	 * 
	 * 
	 * @param lastMessage It must be not null . we have fliter it .
	 * @return
	 */
	private boolean isLastMessage(String lastMessage){
		boolean isSame = false;
		if(softRef_LastMessage==null || softRef_LastMessage.get()==null){
			// do nothing. the result has been "false"
		}else{
			isSame = softRef_LastMessage.get().equalsIgnoreCase(lastMessage) ;//equalsIgnoreCase忽略了大小写
		}
		
		if(!isSame){
			softRef_LastMessage = new SoftReference<String>(lastMessage);
		}
		
		return isSame;
	}
	
	// the last time for show 
	private long lastTime ;
	
	// Maybe , the softReference is not meanigful
	private SoftReference<String> softRef_LastMessage  ;
	
	
	private CustomToast mToast;
	
	private static ToastMaster instance;
	
	// we only need no any instance
	private ToastMaster(){}
	
	public static synchronized ToastMaster getInstance()
    {
		if (instance == null)
		{
			instance = new ToastMaster();
		}
		return instance;
    }
	
	
}
